package com.redteam.data.dao;

import com.redteam.data.beans.CharterCustomer;
import com.redteam.data.beans.HouseAccount;
import com.redteam.data.dao.interfaces.DAO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * DAO for the <code>CharterPilot</code> class
 *
 * @author Steven Lawton
 * @version 1.0
 */
public class CharterCustomerDAO extends AbstractDAO implements DAO {

    private static CharterCustomerDAO dao;
    private static DataSource ds;
    private final String tableName;

    static {
        dao = new CharterCustomerDAO();
    }

    /** {@inheritDoc} */
    public static CharterCustomerDAO getInstance() {
        return dao;
    }

    private CharterCustomerDAO() {
        getDataSource();
        this.tableName = "HIGHFLIERS.PERSONS";
    }

    /** {@inheritDoc} */
    @Override
    public DataSource getDS() {
        return CharterCustomerDAO.ds;
    }

    private void getDataSource() {
        try {
            InitialContext ic = new InitialContext();
            ds = (DataSource) ic.lookup("java:comp/env/jdbc/redteam");

        } catch (NamingException ex) {
            Logger.getLogger(CharterCustomerDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a
     * <code>CharterCustomer</code> bean from
     * <code>ResultSet</code>
     *
     * @param rs
     * <code>ResultSet</code> returned from a query
     * @return
     * <code>CharterCustomer</code> reference
     * @throws SQLException
     */
    @Override
    public CharterCustomer getObjectOut(ResultSet rs) throws SQLException {

        CharterCustomer cc = new CharterCustomer();
        cc.setId(rs.getLong("ID"));
        cc.setFirstName(rs.getString("FIRSTNAME"));
        cc.setMiddleName(rs.getString("MIDDLENAME"));
        cc.setLastName(rs.getString("LASTNAME"));
        cc.setStreet(rs.getString("STREET"));
        cc.setCity(rs.getString("CITY"));
        cc.setState(rs.getString("STATE"));
        cc.setZipCode(rs.getString("ZIPCODE"));
        cc.setEmail(rs.getString("EMAIL"));
        cc.setfBOId(rs.getLong("FBO_ID"));
        cc.setBirthDate(rs.getDate("BIRTHDATE"));
        cc.setHomePhone(rs.getString("HOMEPHONE"));
        cc.setCellPhone(rs.getString("CELLPHONE"));
        cc.setWorkPhone(rs.getString("WORKPHONE"));
        cc.setGender(rs.getString("GENDER"));
        cc.setDiscriminator(rs.getString("DISCRIMINATOR"));
        cc.setUsername(rs.getString("USER_NAME"));
        cc.setPassword(rs.getString("PASSWORD"));
        return cc;
    }

    /**
     * Returns insert query for
     * <code>CharterCustomer</code> provided
     *
     * @param objectToInsert
     * <code>CharterCustomer</code> object to insert into database
     * @return query to insert
     */
    @Override
    public String buildInsertQuery(Object objectToInsert) {
        CharterCustomer person = (CharterCustomer) objectToInsert;
        StringBuilder sb = new StringBuilder();
        sb.append("INSERT INTO ").append(this.getTableName());
        sb.append("(FIRSTNAME,MIDDLENAME,LASTNAME,STREET,CITY,STATE,ZIPCODE,EMAIL,");
        sb.append("FBO_ID,BIRTHDATE,HOMEPHONE,CELLPHONE,WORKPHONE,GENDER,DISCRIMINATOR,USER_NAME,PASSWORD)");
        sb.append(" VALUES ('");
        sb.append(person.getFirstName()).append("','");
        sb.append(person.getMiddleName()).append("','");
        sb.append(person.getLastName()).append("','");
        sb.append(person.getStreet()).append("','");
        sb.append(person.getCity()).append("','");
        sb.append(person.getState()).append("','");
        sb.append(person.getZipCode()).append("','");
        sb.append(person.getEmail()).append("',");
        sb.append(person.getfBOId()).append(",'");
        sb.append(person.getBirthDate()).append("','");
        sb.append(person.getHomePhone()).append("','");
        sb.append(person.getCellPhone()).append("','");
        sb.append(person.getWorkPhone()).append("','");
        sb.append(person.getDiscriminator()).append("','");
        sb.append(person.getGender()).append("','");
        sb.append(person.getUsername()).append("','");
        sb.append(person.getPassword()).append("')");

        return new String(sb);
    }

    /**
     * Returns update query for
     * <code>CharterCustomer</code> provided
     *
     * @param objectToUpdate
     * <code>CharterCustomer</code> object to update in the database
     * @return query to update
     */
    @Override
    public String buildUpdateQuery(Object objectToUpdate) {
        CharterCustomer person = (CharterCustomer) objectToUpdate;
        StringBuilder sb = new StringBuilder("UPDATE ");
        sb.append(this.getTableName()).append(" SET FIRSTNAME = '").append(person.getFirstName());
        sb.append("',MIDDLENAME='").append(person.getMiddleName());
        sb.append("',LASTNAME='").append(person.getLastName()).append("',STREET='");
        sb.append(person.getStreet()).append("',CITY='").append(person.getCity());
        sb.append("',STATE='").append(person.getState()).append("',ZIPCODE='");
        sb.append(person.getZipCode()).append("',EMAIL='").append(person.getEmail());
        sb.append("',FBO_ID=").append(person.getfBOId());
        sb.append(",BIRTHDATE='").append(person.getBirthDate());
        sb.append("',HOMEPHONE='").append(person.getHomePhone());
        sb.append("',CELLPHONE='").append(person.getCellPhone());
        sb.append("',WORKPHONE='").append(person.getWorkPhone());
        sb.append("',GENDER='").append(person.getGender());
        sb.append("',DISCRIMINATOR='").append(person.getDiscriminator());
        sb.append("',USER_NAME='").append(person.getUsername());
        sb.append("',PASSWORD='").append(person.getPassword());
        sb.append("',HOUSE_ACCOUNT=").append(person.getHouseAccount().getAccountNumber());
        sb.append(" WHERE ID= ").append(person.getId());

        return new String(sb);
    }

    /**
     * Returns the ID based on the <code>Object</code>
     *
     * @param obj reference to an <code>CharterCustomer</code>
     * @return id of specified <code>CharterCustomer</code>
     */
    @Override
    public long getObjectId(Object obj) {
        CharterCustomer customer = (CharterCustomer) obj;
        return customer.getId();
    }

    /** {@inheritDoc} */
    @Override
    public String getTableName() {
        return this.tableName;
    }

    /** {@inheritDoc} */
    @Override
    public List<Object> getAll() throws SQLException {
        return getAll("CharterCustomer");
    }

    /** {@inheritDoc} */
    @Override
    public Object getSingle(long id) throws SQLException {
        return getSingle(id, "CharterCustomer");
    }
}
